﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Model;
using System.IO;

namespace Eleme.UI.Controllers
{
    public class HomeController : Controller
    {
        public static string Constring = ConfigurationManager.ConnectionStrings["ConnectionDefault"].ToString();
        // GET: Home

        public ActionResult RegistEleme()
        {
            return View();
        }
        #region 登录

        public ActionResult Login()
        {
            return View();
        }
        #endregion


        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Order()
        {
            return View();
        }
        public ActionResult Order1()
        {
            return View();
        }

        public ActionResult MerchantsShow()
        {
            return View();
        }
        public int ShowMerchantsID(string name)
        {
            using (SqlConnection conn = new SqlConnection(Constring))
            {
                conn.Open();

                string sql = "select MerchantsID from MerchantsTable where MerchantsName='"+name+"'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                ada.Fill(dt);

                MerchantModel model = new MerchantModel();
                model.MerchantsID = Convert.ToInt32(dt.Rows[0]["MerchantsID"]);
                Session["id"] = model.MerchantsID;
                return model.MerchantsID;
            }
        }
        /// <summary>
        /// 菜品添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DishesAdd(FoodModel model)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.Open();
                string sql= "insert into FoodTable values('"+model.FoodName+"','"+model.FoodPrice+"','"+model.FoodIn+" ','"+model.FoodType+"','"+model.FoodState+"','"+model.MerchantsID+"','"+model.PhotoName+"','"+model.PhotoPath+"','"+model.HPhotoName+"')";

                SqlCommand cmd = new SqlCommand(sql, conn);

                int i = cmd.ExecuteNonQuery();

                return i;
            }
        }

        #region 店铺信息添加
        /// <summary>
        /// 店铺添加默认
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(MerchantModel model)
        {
            model.UserID = ShowUID(model.UserNum);

            using (SqlConnection conn = new SqlConnection(Constring))
            {
                conn.Open();


                string sql = "insert into MerchantsTable values('" + model.MerchantsName + "','" + model.MerchantsAddress + "','" + model.SPhone + "','" + model.UserID + "','" + model.BPhotoName + "','" + model.SPhotoName + "','" + model.SPhotoPath + "','" + model.MerchantsType + "','" + model.MerchantsState + "')";

                SqlCommand cmd = new SqlCommand(sql, conn);
                Session["id"] = model.MerchantsID;
                int i = cmd.ExecuteNonQuery();

                return i;
            }
        }
        /// <summary>
        /// 查询UserID
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public int ShowUID(string num)
        {
            using (SqlConnection conn = new SqlConnection(Constring))
            {
                conn.Open();

                string sql = "select UserID from UserTable where UserNum='" + num + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                ada.Fill(dt);

                UsersModel model = new UsersModel();
                model.UserID = Convert.ToInt32(dt.Rows[0]["UserID"]);
                Session["id"] = model.UserID;
                return model.UserID;
            }
        }
        /// <summary>
        /// 修改店铺信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Upt(MerchantModel model)
        {
            model.UserID = ShowUID(model.UserNum);
            int id =Convert.ToInt32(Session["id"]);
            using (SqlConnection conn = new SqlConnection(Constring))
            {
                conn.Open();
                HttpPostedFileBase filebase = Request.Files["SPhotoName"];

                string s = filebase.FileName;

                string path = "/Photo/";

                string filepath = path + s;

                if (Directory.Exists(Server.MapPath(path)) == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(Server.MapPath(path));
                }

                filebase.SaveAs(Server.MapPath(filepath));

                model.SPhotoName = s;

                string sql = "update MerchantsTable set MerchantsName='" + model.MerchantsName + "', MerchantsAddress='" + model.MerchantsAddress + "',SPhone='" + model.SPhone + "',BPhotoName='" + model.BPhotoName + "',SPhotoName='" + model.SPhotoName + "',SPhotoPath='  ',MerchantsType='" + model.MerchantsType + "',MerchantsState=1 where Userid='"+ id + "'";

                SqlCommand cmd = new SqlCommand(sql, conn);

                int i = cmd.ExecuteNonQuery();

                return i; 
            }
        }
        /// <summary>
        /// 店铺完善页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Merchants()
        {      
            
            return View();
        }
        #endregion

      

    }
}